$(function () {
	var useragent = navigator.userAgent;
	
	/*
	if (useragent.match(/MicroMessenger/i) != 'MicroMessenger') {
		// 这里警告框会阻塞当前页面继续加载
		alert('您必须使用微信内置浏览器访问本页面！');
		// 以下代码是用javascript强行关闭当前页面
		var opened = window.open('about:blank', '_self');
		opened.opener = null;
		opened.close();
	}*/
	
		$("#birthday").calendar({
			value: ['1992-12-05']
		});

		
		//begin 上传头像
		/*$(".j-clipHead-input").bind("change",function(){
			$.popup('.popup-clip-head');
		})*/
		$("#j-head-clipArea").photoClip({
			width: 400,
			height: 400,
			//outputType:"png",
			file: "#j-imgHead",
			view: "#j-imgHead-view",
			ok: "#j-sureHead-clip",
			loadStart: function() {
				//console.log("照片读取中");
				$.showPreloader('图片读取中...');
			},
			loadComplete: function() {
				//console.log("照片读取完成");
				$.hidePreloader();
				$.popup('.popup-clip-head');
			},
			clipFinish: function(dataURL) {
				//todo 上传图片应该提交到后台
				$.closeModal('.popup-clip-head');
				$.showPreloader('图片上传中！');
				$.post("http://localhost:8080/icons/uploadjpg.jsp",{file:dataURL},function(data){
					if(data.code == 200){
						$("#showIndex").val(data.result.path);
					}else{
						$("#j-imgHead-view").css({
							"background": "transparent"
						});
						$.toast(data.msg);
					}
					$.hidePreloader();
				});
			}
		});
		//end 上传头像

		//begin  上传图片
		/*$(".j-clipDetails-input").bind("change",function(){
			$.popup('.popup-clip-details');
		})*/
		$("#j-details-clipArea").photoClip({
			width: 400,
			height: 400,
			file: "#j-imgDetails",
			ok: "#j-sureDetails-clip",
			loadStart: function() {
				//console.log("照片读取中");
				$.showPreloader('图片读取中...');
			},
			loadComplete: function() {
				//console.log("照片读取完成");
				$.hidePreloader();
				$.popup('.popup-clip-details');
			},
			clipFinish: function(dataURL) {
				$.closeModal('.popup-clip-details');
				$.showPreloader('图片上传中！')
				//上传图片
				$.post("http://localhost:8080/icons/uploadjpg.jsp",{file:dataURL},function(data){
					if(data.code == 200){
						var imgTmpl = '<div class="col-33 j-img-preview"><div class="img-preview"><input type="checkbox" name="imagePath" checked host="'+data.result.host+'" value="' + data.result.path + '"/> <div class="img-preview-view  j-see-img" style="background-image:url(' + dataURL + ')"></div><span class="img-preview-del">X</span></div></div>'
						$(".j-imgPreview-list").before(imgTmpl);
					}else{
						$.toast(data.msg);
					}
					$.hidePreloader();
				})
				//删除图片
				$(".img-preview-del").live("click",function(){
					$(this).parents(".j-img-preview").remove()
				})
			}
		});
		//end    上传图片

		//begin 查看大图
		$(".j-see-img").live('click',function(){
			var photos = [];
			var inputPath = $("input[name='imagePath']");

			for(var i = 0;i<inputPath.length;i++){
				photos.push($(inputPath[i]).attr('host') + '/' + $(inputPath[i]).val());
			}
			var myPhotoBrowserStandalone = $.photoBrowser({
				photos : photos
			})
			myPhotoBrowserStandalone.open();
		})

		//提交表单
		$("#j-entered-form").submit(function(e){
			var that = this;
			var formData = $("#j-entered-form").serializeArray();

			$("#j-entered-form").ValidateForm({
				success:function(){
					if($.trim($("#showIndex").val()).length == 0){
						$.toast("请上传自己的封面照片");
						return false;
					}

					if($("input[name='imagePath']").length == 0){
						$.toast("请上传本人照片");
						return false;
					}else if($("input[name='imagePath']").length < 3){
						$.toast("请至少上传3张本人照片");
						return false;
					}else if($("input[name='imagePath']").length > 5){
						$.toast("最多上传5张本人照片");
						return false;
					}

					var reg_num = /v\.qq\.com/;
					if($.trim($("textarea[name='video']").val())){
						if(!reg_num.test($("textarea[name='video']").val())){
							$.toast("视频地址只能输入腾讯视频！");
							return false;
						}
					}

					$.confirm('信息一旦提交后，将无法修改。请您确认您的信息填写真实，正确!', '向右转平台提醒您',
						function () {
							$(that).addClass('disabled')
							$.post("register",formData,function(data){
								if(data.code == 200){
									$.router.load("success");
								}else{
									$.toast(data.msg);
								}
								$(that).removeClass("disabled")
							})
						}
					);
				}
			});
			e.preventDefault();
		});

		
	$.init();
})